<?php
declare(strict_types=1);
namespace App\Question;
use App\Controller;
use Field\QuestionThreadConst;
use Field\SysCateConst;

// hook controller_users_index_use.php

/**
 * @title   问答
 * @auth    true
 * @rank    10
 */
class Index extends Controller
{
    // hook controller_users_index_public_start.php

    // hook controller_users_index_public_end.php

    /**
     * @title    首页
     * @rank    99
     */
    public function action_Index()
    {
        $catePinyin = $this->request->param('name', '');
        $catePinyin2 = $this->request->param('name2', '');
        $page = $this->request->param('page', 1);
        $title=[];
        $cond=[];
        $DataCate = $this->SysCate->DataGroup['question'];
        $RootCate = reset($DataCate);
        $keyCate =!empty($RootCate['children']) ? arrlist_change_key($RootCate['children'], SysCateConst::Pinyin):[];
        $path = '/question';
        if(!empty($catePinyin) && empty($keyCate[$catePinyin])){
            $this->response('0301',[],'分类不存在','/question',301);
        }
        if(!empty($keyCate[$catePinyin])){
            $path.='/'.$catePinyin;
            if(!empty($keyCate[$catePinyin]['children'])){
                $CateIDs = arrlist_values($keyCate[$catePinyin]['children'], SysCateConst::CateID);
                $CateIDs[]=$keyCate[$catePinyin]['CateID'];
                $cond[QuestionThreadConst::CateID] = $CateIDs;
            }else{
                $cond[QuestionThreadConst::CateID] = $keyCate[$catePinyin]['CateID'];
            }
            $title[] = $keyCate[$catePinyin]['Name'];
        }
        $CurrentCate=!empty($keyCate[$catePinyin]) ? $keyCate[$catePinyin] : [];
        $keySonCate = !empty($CurrentCate) ? arrlist_change_key($CurrentCate['children'], SysCateConst::Pinyin):[];
        if(!empty($catePinyin2) && empty($keySonCate[$catePinyin2])){
            $this->response('0301',[],'分类不存在','/question/'.$catePinyin,301);
        }
        if(!empty($keySonCate[$catePinyin2])){
            $cond[QuestionThreadConst::CateID] = $keySonCate[$catePinyin2]['CateID'];
            $title[] = $keySonCate[$catePinyin2]['Name'];
            $path.='/'.$catePinyin2;
        }
        $List = $this->QuestionThread->GetList($cond, [QuestionThreadConst::ThreadID => -1], $page, 20);
        $page>1 && $title[] = '第'.$page.'页';
        $title[] = $RootCate['Name'];
        $List['total']=10000;
        $pagination = pagination("{$path}/{page}", $List['total'], $page, 20);
        return $this->View(get_defined_vars());
    }

    /**
     * @title   答案详情
     * @rank    99
     */
    public function action_Detail()
    {
        return $this->View(get_defined_vars());
    }
      /**
     * @title  提交问题
     * @rank    99
     */
    public function action_Add()
    {
        return $this->View(get_defined_vars());
    }

    // hook controller_users_index_end.php
}
?>